import { defineStore } from "pinia";
import { Add_video, Get_list, put_one, delete_one } from "@/apis/VideoApi"
import { type Video, type Evideo } from "@/types/Video"
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
const VideoStore = defineStore("vidoe", () => {
    const router = useRouter()
    const video_add = async (data: Video) => {
        const result: any = await Add_video(data)
        console.log(result)
        if (result.code == 200) {
            ElMessage({
                type: "success",
                message: "添加成功"
            })
            router.back()
        } else {
            ElMessage({
                type: "error",
                message: "添加失败"
            })
        }
    }
    const Get_list_vidoe = async (id: number) => {
        const result: any = await Get_list(id)
        if (result.code == 200) {

            return {
                data: result.data,
                count: result.count
            }
        } else {
            ElMessage({
                type: "error",
                message: "请求失败"
            })
        }
    }
    const put_video = async (data: Evideo) => {
        const result: any = await put_one(data)
        if (result.code == 200) {
            ElMessage({
                type: "success",
                message: "修改成功"
            })
            router.back()
        } else {
            ElMessage({
                type: "error",
                message: "修改失败"
            })
        }
    }
    const delete_video = async (id: string) => {
        const result: any = await delete_one(id)
        if (result.code == 200) {
            ElMessage({
                type: "success",
                message: "删除成功"
            })
        } else {
            ElMessage({
                type: "error",
                message: "删除失败"
            })
        }
    }
    return {
        video_add,
        Get_list_vidoe,
        put_video,
        delete_video
    }
})

export default VideoStore